Page({
    /**
     * 页面的初始数据
     */
    data: {
        ctx: {},
        position: "front",
        iknow: true,
        temPhoto: "",
        agressTxt: "[ 我们将保障您的个人信息安全,不会泄露您的拍摄照片 ]",
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if(getApp().checkIsLogin()){
            wx.getSystemInfo({
                success: res => {
                    // console.log(res)
                    this.setData({
                        CaHeight: res.screenHeight - 105 + "px",
                        statusBarHeight: res.statusBarHeight
                    })
                },
            })
            let ctx = wx.createCameraContext()
            this.setData({
                ctx: ctx
            })
            let openid = wx.getStorageSync('ACCESS_TOKEN')
            let nickname = wx.getStorageSync('appUserInfo').nickName
            let avatar = wx.getStorageSync('appUserInfo').avatarUrl
            let unionid = wx.getStorageSync('appUserInfo').union_id
            let base64String = ""
        }else{
            getApp().doLogin()
        }

    },
    cancelNotice() {
        this.setData({
            iknow: false
        })
    },
    naviback() {
        wx.navigateBack({
            delta: 1,
        })
    },
  
    // 镜头转换
    exchangeJT() {
        // console.log("12")
        let position = this.data.position
        this.setData({
            position: position == 'back' ? 'front' : 'back'
        })
    },
    // 预览
    previewImg() {
        wx.previewImage({
            urls: [this.data.temPhoto],
        })
    },
    
    // 打开相册
    openXC(){
        wx.chooseImage({
          count: 1,
          sourceType:["album"],
          sizeType:['original','compressed'],
          success:res1=>{
            this.setData({
                temPhoto:res1.tempFilePaths[0]
            })
            getApp().globalData.tempPhoto=res1.tempFilePaths[0]           
            // console.log(res1)
            let url = res1.tempFilePaths[0]
            wx.getFileSystemManager().readFile({
                filePath: url, //选择图片返回的相对路径
                encoding: 'base64', //编码格式
                success: res => { //成功的回调
                    // console.log('data:image/png;base64,' + res.data)
                    this.setData({
                        src: 'data:image/png;base64,' + res.data
                    })
                    let data = {
                        openid: wx.getStorageSync('ACCESS_TOKEN'),
                        nickname: wx.getStorageSync('appUserInfo').nickName,
                        avatar: wx.getStorageSync('appUserInfo').avatarUrl,
                        unionid: wx.getStorageSync('appUserInfo').union_id,
                        base64String: res.data
                    }

               
                    wx.navigateTo({
                        url: '../csfa/index',
                      })
                  
                }
            })


          }
          
        })
    },
    takePhoto() {
        if (this.data.iknow) {
            this.cancelNotice()
            return false
        }
        let ctx = this.data.ctx
        ctx.takePhoto({
            quality: 'normal',
            success: (res) => {
                let url = res.tempImagePath;
                this.setData({
                    temPhoto: res.tempImagePath
                })
                getApp().globalData.tempPhoto=url              
                wx.getFileSystemManager().readFile({
                    filePath: url, //选择图片返回的相对路径
                    encoding: 'base64', //编码格式
                    success: res => { //成功的回调
                        // console.log('data:image/png;base64,' + res.data)
                        this.setData({
                            src: 'data:image/png;base64,' + res.data
                        })
                       
                        let data = {
                            openid: wx.getStorageSync('ACCESS_TOKEN'),
                            nickname: wx.getStorageSync('appUserInfo').nickName,
                            avatar: wx.getStorageSync('appUserInfo').avatarUrl,
                            unionid: wx.getStorageSync('appUserInfo').union_id,
                            base64String: res.data
                        }
                        
                

                        wx.navigateTo({
                          url: '../csfa/index',
                        })
                     
                    }
                })
            }
        })
    }
})